package com.ams.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ams.pojo.AsUser;
import com.ams.service.LoginService;
import com.ams.validate.LoginGroup;

@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	@RequestMapping("/login")
	public String login(@Validated(LoginGroup.class) AsUser asUser,BindingResult resulet,HttpServletRequest request,HttpSession session){
		if(resulet.hasErrors()){
			request.setAttribute("loginerr", "notok");
			return "login";
		}
		AsUser user=loginService.selectAsUserByLogin(asUser);
		if(user==null){
			request.setAttribute("loginerr", "nouser");
			return "login";
		}else {
			session.setAttribute("asuser", user);
			return "main";
		}
	}
}
